package com.ilancuo.datetime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间日期工具类
 * Created by ilancuo on 2018/4/8.
 */
public class DateTimeUtil {

    /**
     * 字符串转毫秒值
     */
    public static long str2Mills(String timeStr) {
        try {
            DateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            return date.parse(timeStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 将日期时间格式化成"今天 HH:mm:ss"或者"昨天 HH:mm:ss"或者"yyyy-MM-dd HH:mm:ss"
     *
     * @param time 时间
     */
    public static String formatTime(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (time == null || "".equals(time))
            return "";
        try {
            Date date = format.parse(time);
            Calendar current = Calendar.getInstance();
            current.setTime(date);

            Calendar today = Calendar.getInstance(); //今天
            today.set(Calendar.YEAR, current.get(Calendar.YEAR));
            today.set(Calendar.MONTH, current.get(Calendar.MONTH));
            today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
            today.set(Calendar.HOUR_OF_DAY, 0); // Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);

            Calendar yesterday = Calendar.getInstance(); //昨天
            yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
            yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
            yesterday.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) - 1);
            yesterday.set(Calendar.HOUR_OF_DAY, 0);
            yesterday.set(Calendar.MINUTE, 0);
            yesterday.set(Calendar.SECOND, 0);

            if (current.after(today)) //是不是应该加上小于明天的判断???
                return "今天" + time.split(" ")[1];
            else if (current.before(today) && current.after(yesterday))
                return "昨天" + time.split(" ")[1];
            else {
                int index = time.indexOf("-") + 1;
                return time.substring(index);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取每隔半小时的时间
     */
    public static List<String> getTimeByHalfHour(float start, float end) {
        List<String> time = new ArrayList<>();
        while (start <= end) {
            int temp = (int) start;
            int minute = (int) ((start - temp) * 60);
            if (minute == 0)
                time.add(temp + ":0" + minute);
            else
                time.add(temp + ":" + minute);
            start += 0.5;
        }
        return time;
    }

    /**
     * 与当前日期相加减,传入正数表示相加,负数表示相减
     */
    public static String nowadaysOffset(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + num);
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取星期
     */
    public static String getWeekday() {
        String week[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return week[w];
    }

    /**
     * 获取从今天起, offset 天后的星期
     */
    public static String getWeekday(int offset) {
        String week[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        int w = (calendar.get(Calendar.DAY_OF_WEEK) - 1 + offset) % 7;
        if (w < 0) w = 0;
        return week[w];
    }

    /**
     * 获取从今天起，某一周的日期和周几
     *
     * @param weekOffset 第几周：0，代表当前周；
     */
    public static List<Map<String, String>> getDateAndWeek(int weekOffset) {
        List<Map<String, String>> dateAndWeek = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("day", nowadaysOffset(i + 7 * weekOffset));
            map.put("week", getWeekday(i));
            dateAndWeek.add(map);
        }
        return dateAndWeek;
    }

    /**
     * 获取纯数字字符串格式的日期 2005, 8, 9 -> 20050809
     */
    public static String getStdDateString(int year, int month, int day) {
        StringBuilder result = new StringBuilder();
        result.append(year);
        if (month < 10) {
            result.append(0).append(month);
        } else {
            result.append(month);
        }
        if (day < 10) {
            result.append(0).append(day);
        } else {
            result.append(day);
        }
        return result.toString();
    }

    /**
     * 获取某天是一年中的第几天
     */
    public static int getDayOfYear(int year, int month, int day) {
        int[] dayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int sum = 0;
        if (month == 1) {
            return day;
        }
        if (isLeapYear(year)) {
            for (int i = 1; i < month; i++) {
                sum += dayOfMonth[i - 1];
            }
            sum += day;
            if (month > 2) {
                sum++;
            }
        } else {
            for (int i = 1; i < month; i++) {
                sum += dayOfMonth[i - 1];
            }
            sum += day;
        }
        return sum;
    }

    /**
     * 是否是闰年
     */
    public static boolean isLeapYear(int year) {
        return (year % 400 == 0 || year % 100 != 0 && year % 4 == 0);
    }

    public static void main(String[] args) {
        System.out.println(getDayOfYear(2018, 4, 8));
    }
}
